package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IHotDiscussDao;
import com.magicode.cms.entiy.base.HotDiscuss;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsBroadPlatformRela;
import com.magicode.cms.entiy.base.NewsProgramRela;
import com.magicode.cms.entiy.base.NewsTeleplayRela;
import com.magicode.cms.entiy.base.Picture;
import com.magicode.cms.service.IHotDiscussService;
import com.magicode.common.exception.BusinessException;

@Service("hotDiscussservice")
public class HotDiscussServiceImpl  extends BaseServiceImpl<HotDiscuss, IHotDiscussDao> implements IHotDiscussService{
	@Autowired
	@Qualifier("hotDiscussdao")
	private IHotDiscussDao hotDiscussDao;
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +HotDiscuss.class.getName()+" "+entity_alias_name+" ";

		hql += " left join "+entity_alias_name+".topic as tp ";
		hql += " left join "+entity_alias_name+".author as au ";
		if(null!=map && !map.isEmpty()){
			hql += "where 1 = 1  ";  //and "+entity_alias_name+".isContribute = 0
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, " 热门标题");
		map.put("width", 180);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "topicTitle");
		map.put(json_title, "评论话题");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "sourceStr");
		map.put(json_title, "来源");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "subcribeTime");
		map.put(json_title, "发布日期");
		map.put("width", 90);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public IHotDiscussDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return hotDiscussDao;
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/hotDiscuss/sec/"+id+"");
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}

	
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("topicTitle", "tp.title as topicTitle ");
			gridsel.put("sourceStr", ""+entity_alias_name+".sourceStr as sourceStr ");
			gridsel.put("subcribeTime", sql_date_method+"("+entity_alias_name+".subcribeTime  ,'"+sql_time_format+"' ) as subcribeTime ");				
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	/**
	 * 更新频道排序规则
	 * 获得所有视频排序类型大于等于新闻，但不等于100的数据
	 * @param article
	 */
	@SuppressWarnings({ "unused", "unchecked" })
	synchronized public void  updateChannelOrderType(HotDiscuss article){
		String id = article.getId();
		String sql = "  select t from com.magicode.cms.entiy.base.Article t "
				+ " where t.channelOrderTypeInt != 100 and t.channelOrderTypeInt >= "+article.getChannelOrderTypeInt()+" "
						+ "and t.id != '"+id+"' ";
//		获得所有视频排序类型大于等于新闻，但不等于100的数据
		List<Object> articleList = this.queryDataList(sql);

		Integer tempOrderType = article.getChannelOrderTypeInt();
		for(Object obj : articleList){
			if(obj instanceof HotDiscuss){
				HotDiscuss tempNews = (HotDiscuss) obj;
				Integer typeInt = tempNews.getChannelOrderTypeInt();
				tempOrderType++;
				if((tempOrderType) > 3 ){
					tempOrderType = 100;
				}
				tempNews.setChannelOrderTypeInt(tempOrderType);
//				this.
//				this.editObject(tempNews);
				try {
					this.getDao().getCommonDaoService().merge(tempNews);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unused" })
	@Override
	public void setupHead(News news) throws BusinessException {
		// TODO Auto-generated method stub
		String id = news.getId() ;
				if(news.getHead() != null && news.getHead() == 1){ 
					String hql = "select t from " +HotDiscuss.class.getName()+" t where t.head = 1";
					Map map = new HashMap();
					if(id  != null && id.trim().length() > 0){
						hql += "and t.id != '"+id+"'";
//						map.put("id", id);
						
					}
					List<Object> list = null;
//					if(map.isEmpty()){
						list = this.queryDataList(hql);
//					}
//					else{
//						list = this.queryDataList(hql,map);
//					}
					for(Object otherNews : list){
						if(otherNews instanceof News){
							((News)otherNews).setHead(0);
							try {
								this.getDao().editObject((News) otherNews);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								throw new BusinessException(e);
							}
						}
					}
		}
	}
	
	//循环删除
	@Override
	public String deleteAll(Map<String, String> map) throws Exception {
		String hql ="from " +HotDiscuss.class.getName()+" "+entity_alias_name+" ";
		while(null!=map && !map.isEmpty()){
			hql = "delete from"+entity_alias_name+".id = '"+map.get(field_id)+"'";
			System.out.println(hql);
		}
		return hql;
	}

	@Override
	public List<NewsProgramRela> getNewsProgramRela(String id) {
		List<NewsProgramRela> videoList = new ArrayList();
		HotDiscuss enterprise = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		String[] fetchJoins = new String[3];
		fetchJoins[0] = "mainPic";
		fetchJoins[1] = "author";
		fetchJoins[2] = "programList";
		try {
			enterprise =super.getObject(id,fetchJoins);
		} catch (Exception e) {
			e.printStackTrace();
		}
		videoList = enterprise.getProgramList();
		return videoList;
	}
	
	@Override
	public List<NewsTeleplayRela> getNewsTeleplayRela(String id) {
		List<NewsTeleplayRela> videoList = new ArrayList();
		HotDiscuss enterprise = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		String[] fetchJoins = new String[3];
		fetchJoins[0] = "mainPic";
		fetchJoins[1] = "author";
		fetchJoins[2] = "teleplayList";
		try {
			enterprise =super.getObject(id,fetchJoins);
		} catch (Exception e) {
			e.printStackTrace();
		}
		videoList = enterprise.getTeleplayList();
		return videoList;
	}
	@Override
	public List<NewsBroadPlatformRela> getNewsBroadPlatformRela(String id) {
		List<NewsBroadPlatformRela> videoList = new ArrayList();
		HotDiscuss enterprise = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		String[] fetchJoins = new String[3];
		fetchJoins[0] = "mainPic";
		fetchJoins[1] = "author";
		fetchJoins[2] = "platformList";
		try {
			enterprise =super.getObject(id,fetchJoins);
		} catch (Exception e) {
			e.printStackTrace();
		}
		videoList = enterprise.getPlatformList();
		return videoList;
	}
	/*
	 *上传多个配图
	 */
	@Override
	public List<Picture> getPictures(String id) {
		List<Picture> picList = new ArrayList();
		String sql = " select p from "+Picture.class.getName()+" p , "+News.class.getName()+" t "
				+ " where p in elements(t.relaPics) and t.id = :id order by p.id ";
		HotDiscuss tvFilmPerson = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			picList = this.hotDiscussDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return picList;
	}
	public IHotDiscussDao getHotDiscussDao() {
		return hotDiscussDao;
	}
	public void setHotDiscussDao(IHotDiscussDao hotDiscussDao) {
		this.hotDiscussDao = hotDiscussDao;
	}
	@Override
	public HotDiscuss getInfo(String id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public long findCountArticleTpic(String id) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
}
